import request from "../../utils/request";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    videoGroupList: {}, // 视频分类列表
    navId: "", // 导航id
    videoList: [], // 视频列表
    videoId: "",
    videoUpdateTime: [], // 视屏播放时间
    isRefresh: false, // 视频列表是否下拉刷新
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getVideoGroupList();
  },

  // 获取视屏列表
  async getVideoGroupList() {
    let videoGroupList = await request("video/group/list");
    videoGroupList = videoGroupList.data.splice(0, 14);
    this.setData({ videoGroupList, navId: videoGroupList[0].id }); // 默认第一个添加类名active
    this.getVideoList(this.data.navId);
  },

  // 导航点击事件回调
  changeNav(e) {
    wx.showLoading({ title: "正在加载" });
    this.setData({ videoList: null });
    const navId = e.currentTarget.id;
    this.setData({ navId: navId >>> 0 });
    this.getVideoList(navId);
  },

  // 获取导航分类视频
  async getVideoList(id) {
    const res = await request("video/group", { id });
    // console.log(res);
    const videoList = res.datas.map((item, index) => {
      item.id = index;
      return item;
    });
    // console.log(videoList);
    this.setData({ videoList, isRefresh: false });
    wx.hideLoading();
  },

  // 播放/暂停视视频
  handleVideoPlay(e) {
    let vid = e.currentTarget.id;
    // this.vid !== vid && this.video && this.video.pause();
    // this.vid = vid;
    this.setData({ videoId: vid });
    this.video = wx.createVideoContext(vid);
    let videoUpdateTime = this.data.videoUpdateTime;
    const item = videoUpdateTime.find((item) => item.vid === vid);
    if (item) {
      this.video.seek(item.currentTime);
    }
    this.video.play();
  },

  // 视屏播放时的回调
  handleTimeUpdate(e) {
    // console.log(e);
    // 获取当前视频播放记录
    const timeUpdateObj = {
      vid: e.currentTarget.id,
      currentTime: e.detail.currentTime,
    };
    // 将当前视频对象的播放时间存入数组
    const videoUpdateTime = this.data.videoUpdateTime;
    // 判断当前播放对象在数组里是否存在
    let videoItem = videoUpdateTime.find(
      (item) => item.vid === timeUpdateObj.vid
    );
    if (videoItem) {
      // 存在则修改该视频对象的currentTime
      videoItem.currentTime = timeUpdateObj.currentTime;
    } else {
      videoUpdateTime.push(timeUpdateObj);
    }

    this.setData({ videoUpdateTime });
  },
  // 视屏播放结束回调
  handleEnded(e) {
    let videoUpdateTime = this.data.videoUpdateTime;
    const index = videoUpdateTime.findIndex(
      (item) => item.vid === e.currentTarget.id
    );
    videoUpdateTime.splice(index, 1);
    this.setData({ videoUpdateTime });
  },
  // 视频列表下拉刷新回调
  handleRefresh() {
    // console.log("refresher");
    this.getVideoList(this.data.navId);
  },
  // 视频列表滑动到底部回调
  handleToLower() {
    // console.log("handleToLower");
    const videoList = this.data.videoList;
    videoList.push(...videoList);
    this.setData({ videoList });
  },
  // 跳转到搜索页面
  toPageSearch() {
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    console.log(e.from);
    if (e.from === "menu") {
      return {
        title: "右上角转发",
        path: "/pages/video/video",
        imageUrl: "/static/images/nvsheng.jpg",
      };
    } else {
      return {
        title: "按钮转发",
        path: "/pages/video/video",
        imageUrl: "/static/images/nvsheng.jpg",
      };
    }
  },
});
